package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import sfmc.biz.service.SfmcEmpService;
import sfmc.biz.service.SfmcMainService;
import sfmc.biz.vo.Sfmc_TB_EMPLOYEE;
import sfmc.biz.vo.Sfmc_TB_MAIN;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcMainController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcMainService sfmcMainService;
	
	@Autowired 
	private SfmcEmpService sfmcEmpService;
	
	@RequestMapping(value ="/main.do")
	public ModelAndView list(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_MAIN sfmc_TB_MAIN){

		if("".equals(sfmc_TB_MAIN.getSearchFromDate())){
			sfmc_TB_MAIN.setSearchFromDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_MAIN.setSearchToDate(DateUtil.getCurrentTime("yyyy-MM-dd"));
			sfmc_TB_MAIN.setPageNo(1);
			sfmc_TB_MAIN.setPageSize(20);
			sfmc_TB_MAIN.setOffSet(0);
			sfmc_TB_MAIN.setTotalCnt(0);
		}else{
			sfmc_TB_MAIN.setOffSet((sfmc_TB_MAIN.getPageNo()-1)*sfmc_TB_MAIN.getPageSize());
			sfmc_TB_MAIN.setTotalCnt(sfmcMainService.getRecordCount(sfmc_TB_MAIN));
		}

		List<Sfmc_TB_MAIN> mainList = sfmcMainService.getMainList(sfmc_TB_MAIN);
		
		List<Sfmc_TB_EMPLOYEE> empList = sfmcEmpService.getEmpList(new Sfmc_TB_EMPLOYEE());
		
		mv.addObject("mainList", (new Gson()).toJson(mainList));
		mv.addObject("mainData", sfmc_TB_MAIN);
		mv.addObject("empList", empList);
		mv.setViewName("/main/main_list");
		return mv;
	}
	
	@RequestMapping(value ="/main_save.do" , method = RequestMethod.POST)
	public ModelAndView save(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_MAIN sfmc_TB_MAIN){
		
		mv.setViewName("/main/main_result");

		int returnVal = -1;
		String type = "";
		
		returnVal = sfmcMainService.updateMain(sfmc_TB_MAIN);
		type = "U";
		
		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);
		mv.addObject("fileName", sfmc_TB_MAIN.getDataOrdFilename());
		return mv;
	}
	
	@RequestMapping(value ="/main_delete.do" , method = RequestMethod.POST)
	public ModelAndView delete(ModelAndView mv, HttpSession session, HttpServletRequest request, Sfmc_TB_MAIN sfmc_TB_MAIN){
		
		mv.setViewName("/main/main_result");

		int returnVal = -1;
		String type = "D";
		
		if(!"".equals(sfmc_TB_MAIN.getDataStr())){
			returnVal = sfmcMainService.deleteMain(sfmc_TB_MAIN);
		}

		mv.addObject("returnVal", returnVal);
		mv.addObject("type", type);
		return mv;
	}
	
	
	@RequestMapping(value="/mainList_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_MAIN sfmc_TB_MAIN) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_MAIN> excelList = sfmcMainService.getMainList(sfmc_TB_MAIN, true);
		resp.setHeader("Content-Disposition", "attachment; filename=MainList.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			HSSFCell headerD = rowHeader.createCell(3);
			HSSFCell headerE = rowHeader.createCell(4);
			HSSFCell headerF = rowHeader.createCell(5);
			HSSFCell headerG = rowHeader.createCell(6);
			HSSFCell headerH = rowHeader.createCell(7);
			HSSFCell headerI = rowHeader.createCell(8);
			headerA.setCellValue(new HSSFRichTextString("상태"));
			headerB.setCellValue(new HSSFRichTextString("날짜"));
			headerC.setCellValue(new HSSFRichTextString("시간"));
			headerD.setCellValue(new HSSFRichTextString("CID"));
			headerE.setCellValue(new HSSFRichTextString("주문업체"));
			headerF.setCellValue(new HSSFRichTextString("채널"));
			headerG.setCellValue(new HSSFRichTextString("확인날짜"));
			headerH.setCellValue(new HSSFRichTextString("파일명"));
			headerI.setCellValue(new HSSFRichTextString("담당자"));
			
			sfmc_TB_MAIN = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i+1);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			HSSFCell cells4 = rowA.createCell(3);
			HSSFCell cells5 = rowA.createCell(4);
			HSSFCell cells6 = rowA.createCell(5);
			HSSFCell cells7 = rowA.createCell(6);
			HSSFCell cells8 = rowA.createCell(7);
			HSSFCell cells9 = rowA.createCell(8);
			cells1.setCellValue(sfmc_TB_MAIN.getConfirmOrd());
			cells2.setCellValue(sfmc_TB_MAIN.getOrdDate1());
			cells3.setCellValue(sfmc_TB_MAIN.getOrdDate2());
			cells4.setCellValue(sfmc_TB_MAIN.getCallerID());
			cells5.setCellValue(sfmc_TB_MAIN.getCustName());
			cells6.setCellValue(sfmc_TB_MAIN.getChannelNum());
			cells7.setCellValue(sfmc_TB_MAIN.getConfirmDate());
			cells8.setCellValue(sfmc_TB_MAIN.getOrdFilename());
			cells9.setCellValue(sfmc_TB_MAIN.getEmpName());
		}
		
		try{
			workbook.write(ost);
			ost.flush();
			fos = new FileOutputStream(new File("C:/MainList.xls"));
			workbook.write(fos);
		}catch(Exception e){
			log.error(e);
		}finally{
			if(fos != null){
				try{
					fos.flush();
					fos.close();
				}
				catch(IOException e){
					log.error(e);
				}
			}
		}
	}
}
